using System;
using System.Linq;
using Send2Sharepoint.Common;
using Microsoft.Office.Interop.Outlook;
using Exception = System.Exception;

namespace Send2Sharepoint.Outlook
{
    class OutlookFolder
    {
        private OutlookFolder()
        {
        }
        
        internal static MAPIFolder GetRootFolder(Application application)
        {
            try
            {
                return application.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            }
            catch (Exception ex)
            {
                Logging.WriteException(ex);
                throw;
            }
        }

        internal static MAPIFolder FindCreateFolder(MAPIFolder parentFolder, String folderName)
        {
            try
            {
                var folder = FindFolder(parentFolder, folderName) ??
                                    parentFolder.Folders.Add(folderName, OlDefaultFolders.olFolderInbox);
                return folder;
            }
            catch (Exception ex)
            {
                Logging.WriteException(ex);
                throw;
            }
        }

        internal static MAPIFolder FindFolder(MAPIFolder parentFolder, string folderName)
        {
            try
            {
                return parentFolder.Folders.Cast<MAPIFolder>().FirstOrDefault(folder => folder.Name.Equals(folderName));
            }
            catch (Exception ex)
            {
                Logging.WriteException(ex);
                throw;
            }
        }
    }
}
